# -*- coding: utf-8 -*-
import scrapy
import json
import jsonpath
from scrapy.http import Request
from tutorial.items import TrafficJamItem


class TrafficJamSpider(scrapy.Spider):
    name = 'trafficjamspider'
    allowed_domains = ['localhost']
    start_urls = ['http://localhost:3000/json']

    def parse(self, response):
        item = TrafficJamItem()
        rs = json.loads(response.text)
        for i in range(2010, 2020):
            aResule = '$.' + str(i)
            for j in range(1, 4):
                item['num'] = str(jsonpath.jsonpath(rs, aResule + '.person' + str(j))).replace("[", "").replace("]", "")
                item['year'] = i
                item['place'] = 'person' + str(j)
                yield item
